using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace Axe.Collections
{
    public class SyncDictionary<TKey, TValue> : 
        IDictionary<TKey, TValue>,
        ICollection<KeyValuePair<TKey, TValue>>,
        IEnumerable<KeyValuePair<TKey, TValue>>, 
        IDictionary, 
        ICollection, 
        IEnumerable
    {
        private object SyncObj = new object();
        private Dictionary<TKey, TValue> Container;

        public Dictionary<TKey,TValue> Unsafe
        {
            get
            {
                return Container;
            }
        }

        public SyncDictionary()
        {
            Container = new Dictionary<TKey, TValue>();
        }

        public SyncDictionary(IDictionary<TKey, TValue> dictionary)
        {
            Container = new Dictionary<TKey, TValue>(dictionary);
        }

        public SyncDictionary(int initialCapacity)
        {
            Container = new Dictionary<TKey, TValue>(initialCapacity);
        }

        public SyncDictionary(IDictionary<TKey, TValue> dictionary,IEqualityComparer<TKey> comparer)
        {
            Container = new Dictionary<TKey, TValue>(dictionary, comparer);
        }

        public SyncDictionary(int capacity,IEqualityComparer<TKey> comparer)
        {
            Container = new Dictionary<TKey, TValue>(capacity, comparer);
        }

        #region IDictionary<TKey,TValue> Members

        public void Add(TKey key, TValue value)
        {
            lock(SyncObj)
            {
                Container.Add(key, value);
            }
        }

        public bool ContainsKey(TKey key)
        {
            lock(SyncObj)
            {
                return Container.ContainsKey(key);
            }
        }

        public ICollection<TKey> Keys
        {
            get
            {
                lock(SyncObj)
                {
                    return Container.Keys;
                }
            }
        }

        public bool Remove(TKey key)
        {
            lock(SyncObj)
            {
                return Container.Remove(key);
            }
        }

        public bool TryGetValue(TKey key, out TValue value)
        {
            lock(SyncObj)
            {
                return Container.TryGetValue(key, out value);
            }
        }

        public ICollection<TValue> Values
        {
            get
            {
                lock(SyncObj)
                {
                    return Container.Values;
                }
            }
        }

        public TValue this[TKey key]
        {
            get
            {
                lock(SyncObj)
                {
                    return Container[key];
                }
            }
            set
            {
                lock(SyncObj)
                {
                    Container[key] = value;
                }
            }
        }

        #endregion

        #region ICollection<KeyValuePair<TKey,TValue>> Members

        public void Add(KeyValuePair<TKey, TValue> item)
        {
            lock(SyncObj)
            {
                Container.Add(item.Key, item.Value);
            }
        }

        public void Clear()
        {
            lock(SyncObj)
            {
             
            }
        }

        public bool Contains(KeyValuePair<TKey, TValue> item)
        {
            lock(SyncObj)
            {
                return (Container as ICollection<KeyValuePair<TKey, TValue>>).Contains(item);
            }
        }

        public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
        {
            lock(SyncObj)
            {
                (Container as ICollection<KeyValuePair<TKey, TValue>>).CopyTo(array, arrayIndex);
            }
        }

        public int Count
        {
            get
            {
                lock(SyncObj)
                    return Container.Count;
            }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(KeyValuePair<TKey, TValue> item)
        {
            lock(SyncObj)
            {
                return Container.Remove(item.Key);
            }
        }

        #endregion

        #region IEnumerable<KeyValuePair<TKey,TValue>> Members

        public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
        {
            lock(SyncObj)
            {
                return Container.GetEnumerator();
            }
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            lock(SyncObj)
            {
                return Container.GetEnumerator();
            }
        }

        #endregion

        #region IDictionary Members

        public void Add(object key, object value)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public bool Contains(object key)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        IDictionaryEnumerator IDictionary.GetEnumerator()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public bool IsFixedSize
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        ICollection IDictionary.Keys
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        public void Remove(object key)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        ICollection IDictionary.Values
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        object IDictionary.this[object key]
        {
            get
            {
                throw new Exception("The method or operation is not implemented.");
            }
            set
            {
                throw new Exception("The method or operation is not implemented.");
            }
        }

        #endregion

        #region ICollection Members

        void ICollection.CopyTo(Array array, int index)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public bool IsSynchronized
        {
            get { return true; }
        }

        public object SyncRoot
        {
            get { return SyncObj; }
        }

        #endregion
    }
}
